/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

/**
 *
 * @author Administrator
 */
import ejb.GestoreAmministratoreLocal;
import ejb.GestoreStudenteLocal;
import ejb.GestoreTutorLocal;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import jpa.Responsabile;
import jpa.Studente;
import jpa.Tutor;

@ManagedBean(name = "Login")
@SessionScoped
public class Login {

    @EJB
    GestoreStudenteLocal gs;
    @EJB
    GestoreAmministratoreLocal ga;
    @EJB
    GestoreTutorLocal gt;

    private String user;
    private String pass;
    private String tipo;
    private Responsabile r;
    private Studente s;
    private Tutor t;
    private String message;
    private String nome;
    private String cognome;
    private String id;

    public Login() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Responsabile getR() {
        return r;
    }

    public Tutor getT() {
        return t;
    }

    public Studente getS() {
        return s;
    }

    private void setNome(String nome) {
        this.nome = nome;
    }

    private void setCognome(String cognome) {
        this.cognome = cognome;
    }

    public String getNome() {
        return nome;
    }

    public String getCognome() {
        return cognome;
    }

    public String login() {
        switch (tipo) {
            case "amministratore": {
                r = ga.login(user, pass);
                if (r == null) {
                    this.message = "Username e/o Password errate";
                    return "login";
                }
                setCognome(r.getCognome());
                setNome(r.getNome());
                FacesContext context = FacesContext.getCurrentInstance();
                HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
                session.setAttribute("id", r.getId());
                session.setAttribute("username", user);
                session.setAttribute("tipo", "amministratore");
                id = r.getId().toString();
                return "paginaAdmin";
            }
            case "tutor": {
                t = gt.login(user, pass);
                if (t == null) {
                    this.message = "Username e/o Password errate";
                    return "login";
                }
                setCognome(t.getCognome());
                setNome(t.getNome());
                FacesContext context = FacesContext.getCurrentInstance();
                HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
                session.setAttribute("id", t.getId());
                session.setAttribute("username", user);
                session.setAttribute("tipo", "tutor");
                id = t.getId().toString();
                return "paginaTutor";
            }
            default: {
                s = gs.login(user, pass);
                if (s == null) {
                    this.message = "Username e/o Password errate";
                    return "login";
                }
                setCognome(s.getCognome());
                setNome(s.getNome());
                FacesContext context = FacesContext.getCurrentInstance();
                HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
                session.setAttribute("id", s.getId());
                session.setAttribute("username", user);
                session.setAttribute("tipo", "studente");
                id = s.getId().toString();
                return "paginaStud";
            }
        }
    }

    public String logout() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        if (session != null) {
            session.invalidate();
        }
        return "logout";
    }
}
